package com.shop.user.api.controller;

import com.shop.common.entity.user.UserBase;
import com.shop.common.model.ServiceBean;
import com.shop.common.tmpl.BaseController;
import com.shop.common.util.RedisUtil;
import com.shop.common.util.SendCodeUtil;
import com.shop.common.util.SessionUtils;
import com.shop.common.util.excption.UserNotFoundException;
import com.shop.common.util.state.KeyPrefix;
import com.shop.common.util.state.ResultCode;
import com.shop.common.util.state.UserState;
import com.shop.user.api.api.IUserApi;
import com.shop.user.api.model.RestModel;
import com.shop.user.api.model.UserBaseModel;
import com.shop.user.api.service.IUserSvc;
import com.shop.user.atom.atomsvc.IUserAtomSvc;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
public class UserController extends BaseController implements IUserApi {
    final static Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    IUserAtomSvc userService;
    @Autowired
    SessionUtils sessionUtils;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    IUserSvc iUserSvc;

    @Override
    public String simpleUser(HttpServletRequest request) throws Exception {
        UserBase userBase = new UserBase();
        Integer userId = sessionUtils.USER_ID(request);
        if (userId == null) {
            return returnFailed();
        }
        userBase.setUserId(userId);
        ServiceBean serviceBean = userService.selectBaseUser(userBase);
        if (!serviceBean.isSuccess()) {
            return returnMsg(ResultCode.FAILED.value(), serviceBean.getMsg());
        }
        userBase = (UserBase) serviceBean.getData();
        if (!serviceBean.isSuccess()) {
            return returnMsg(ResultCode.FAILED.value(), serviceBean.getMsg());
        }
        UserBase user = new UserBase();
        user.setNickname(userBase.getNickname());
        user.setUserId(userBase.getUserId());
        UserBaseModel userModel = new UserBaseModel(user);
        return returnJSON(ResultCode.SUCCESS.value(), "", userModel);
    }

    @Override
    public String qryUserById(@PathVariable(value = "userId") Integer userId) {
        UserBase userBase = new UserBase();
        userBase.setUserId(userId);
        ServiceBean<UserBase> serviceBean = userService.selectBaseUser(userBase);
        if (serviceBean.isFaild()){
            return returnFailed();
        }
        userBase = serviceBean.getData();
        userBase.clean();
        return returnData(userBase);
    }

    @Override
    public String apiGetUser(HttpServletRequest request) throws UserNotFoundException {
        UserBase userBase = new UserBase();
        Integer userId = sessionUtils.USER_ID(request);
        if (userId == null) {
            return returnFailed();
        }
        userBase.setUserId(userId);
        ServiceBean serviceBean = userService.selectBaseUser(userBase);
        if (!serviceBean.isSuccess()) {
            return returnMsg(ResultCode.FAILED.value(), serviceBean.getMsg());
        }
        userBase = (UserBase) serviceBean.getData();
//        BuyerUser buyerUser=new BuyerUser();
//        buyerUser.setUserId(Integer.valueOf(userId));
//        resultModel=userService.selectBuyerUser(buyerUser);
        if (!serviceBean.isSuccess()) {
            return returnMsg(ResultCode.FAILED.value(), serviceBean.getMsg());
        }
//        buyerUser= (BuyerUser) resultModel.getData();
        UserBaseModel userModel = new UserBaseModel(userBase);
//        userModel.setIntegral(buyerUser.getIntegral());
//        userModel.setLevelId(buyerUser.getLevelId());
//        userModel.setBalance(buyerUser.getBalance());
        return returnJSON(ResultCode.SUCCESS.value(), "", userModel);
    }

    @Override
    public String apiUpdateUser(@RequestBody UserBase userBase, HttpServletRequest request) throws UserNotFoundException {
        log.info("修改用户信息:" + userBase.getAccount() + "--- 用户信息：" + userBase.toString());
        Integer userId = sessionUtils.USER_ID(request);
        UserBase user = userService.selectBaseUserById(userId);
        if (user.getStatus().intValue() == UserState.DELETE.value()|| user.getStatus().intValue() == UserState.ABANDON.value()) {
            return returnFailed("该账户已作废");
        }
        userBase.setStatus(user.getStatus() == UserState.INIT.value() ? UserState.NORMAL.value() : null);
        ServiceBean result = userService.updateBaseUser(userBase);
        if (result.isSuccess()) {
            return returnSuccess();
        } else {
            return returnFailed();
        }
    }

    @Override
    public String apiRestPwd(@RequestBody RestModel restModel, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        restModel.setUserId(userId);
        ServiceBean serviceBean = iUserSvc.restPwd(restModel);
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed(serviceBean.getMsg());
    }

    @Override
    public String apiHasOldPayPwd(HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        ServiceBean<String> serviceBean = iUserSvc.hasPassword(userId, 2);
        if (serviceBean.isSuccess() && StringUtils.isNotBlank(serviceBean.getData())) {
            return returnSuccess();
        }
        return returnFailed();
    }

    @Override
    public String apiQryPhone(HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        ServiceBean serviceBean = iUserSvc.qryUserPhone(userId);
        if (serviceBean.isSuccess()) {
            return returnData(serviceBean.getData());
        }
        return returnFailed();
    }

    @Override
    public String apiQryEmail(HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        ServiceBean serviceBean = iUserSvc.qryUserEmail(userId);
        if (serviceBean.isSuccess()) {
            return returnData(serviceBean.getData());
        }
        return returnFailed();
    }

    @Override
    public String apiRestPayPwd(@RequestBody RestModel restModel, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        restModel.setUserId(userId);
        ServiceBean serviceBean = iUserSvc.restPayPwd(restModel);
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed(serviceBean.getMsg());
    }

    @Override
    public String apiRestPhoneVerify(String phone, Integer type, HttpServletRequest request) {
        ServiceBean serviceBean = new ServiceBean();
        switch (type) {
            case 1:
                serviceBean = SendCodeUtil.SEND_BY_PHONE(KeyPrefix.REST, phone);
                break;
            case 2:
                serviceBean = SendCodeUtil.SEND_BY_EMAIL(KeyPrefix.REST, phone);
        }
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        } else {
            return returnFailed(serviceBean.getMsg());
        }
    }

    @Override
    public String apiRestPhone(@RequestBody RestModel restModel, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        restModel.setUserId(userId);
        ServiceBean serviceBean = iUserSvc.restPhone(restModel);
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed();
    }

    @Override
    public String apiValidateIdcard(@RequestBody RestModel restModel, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        restModel.setUserId(userId);
        ServiceBean serviceBean = iUserSvc.validateIdCard(restModel);
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed();
    }

    @Override
    public String validatePayPwd(@PathVariable(value = "pwd") String pwd, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        pwd = DigestUtils.md5Hex(pwd);
        return returnSvc(iUserSvc.validatePayPwd(userId,pwd));
    }

    @Override
    public String userSecurity(HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        return returnSvc(iUserSvc.qryUserSecurity(userId));
    }

    @Override
    public String userQuit(HttpServletRequest request) {
        sessionUtils.QUIT(request);
        return returnSuccess();
    }

}
